//
//  HBHBannerViewController.m
//  HubeiHotel
//
//  Created by 张露杰 on 18/4/12.
//  Copyright © 2018年 张露杰. All rights reserved.
//

#import "HBHBannerViewController.h"

#import "HBHWebViewCell.h"

@interface HBHBannerViewController ()<UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, retain) NSDictionary *data;

@property (nonatomic) CGFloat webH;
@property (nonatomic, strong) UIWebView *web;

@end

@implementation HBHBannerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    [self getDetail];
}

-(void)creatView
{
    [self initTitleWithTitle:@"消息详情"];
    [self.view addSubview:self.table];
}

-(UIWebView *)web
{
    if (!_web) {
        _web = [[UIWebView alloc]init];
        _web.delegate = self;
    }
    return _web;
}

//Web加载完成
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //计算webView的高
    CGRect frame = webView.frame;
    frame.size.width = SCREENW;
    frame.size.height = 1;
    webView.scrollView.scrollEnabled = NO;
    webView.frame = frame;
    frame.size.height = webView.scrollView.contentSize.height;
    webView.frame = frame;
    _webH = frame.size.height;
    [HudHelper hideHudToView:self.view];
    [_table reloadData];
}

-(UITableView *)table//创建View
{
    if (!_table) {
        _table = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStyleGrouped];
        _table.backgroundColor = [UIColor whiteColor];
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table.showsVerticalScrollIndicator = NO;
        _table.delegate = self;
        _table.dataSource = self;
    }
    return _table;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    
    UILabel *titleLabel = [TomamaUI createLabel:CGRectMake(15*screenW_6, 24*screenW_6, SCREENW - 30*screenW_6, [TomamaUI getStringHeightWith:_data[@"title"] andWeith:SCREENW - 30*screenW_6 andFont:[UIFont boldSystemFontOfSize:22*screenW_6] andLineSpacing:10*screenW_6]) textAligment:0 font:[UIFont boldSystemFontOfSize:22*screenW_6] textColor:TextBlack text:_data[@"title"]];
    [view addSubview:titleLabel];
    titleLabel.numberOfLines = 0;
    if (titleLabel.frame.size.height - 22*screenW_6 > 10*screenW_6) {
        [titleLabel setLineSpaceWithLineSpacing:10*screenW_6 text:_data[@"title"]];
    }
    
    UILabel *timeLabel = [TomamaUI createLabel:CGRectMake(15*screenW_6, CGRectGetMaxY(titleLabel.frame) + 15*screenW_6, SCREENW - 30*screenW_6, 14*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:14*screenW_6] textColor:TextGray text:_data[@"createDate"]];
    [view addSubview:timeLabel];
    
    UIImageView *imageV = [TomamaUI createImageView:CGRectMake(15*screenW_6, CGRectGetMaxY(timeLabel.frame) + 25*screenW_6, SCREENW - 30*screenW_6, 195*screenW_6) backGroundImageV:@""];
    imageV.clipsToBounds = YES;
    imageV.contentMode = UIViewContentModeScaleAspectFill;
    [view addSubview:imageV];
    [imageV sd_setImageWithURL:[NSURL URLWithString:_data[@"image"]] placeholderImage:nil];
    
//    UILabel *label = [TomamaUI createLabel:CGRectMake(15*screenW_6, CGRectGetMaxY(imageV.frame) + 23*screenW_6, SCREENW - 30*screenW_6, [TomamaUI getStringHeightWith:_data[@"content"] andWeith:SCREENW - 30*screenW_6 andFont:[UIFont systemFontOfSize:15*screenW_6] andLineSpacing:13*screenW_6]) textAligment:0 font:[UIFont systemFontOfSize:13*screenW_6] textColor:TextBlack text:_data[@"content"]];
//    [view addSubview:label];
//    [label setLineSpaceWithLineSpacing:13*screenW_6 text:_data[@"content"]];
    view.frame = CGRectMake(0, 0, SCREENW, CGRectGetMaxY(imageV.frame) + 20*screenW_6);
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat h = 0;
    if ([TomamaUI getStringHeightWith:_data[@"title"] andWeith:SCREENW - 30*screenW_6 andFont:[UIFont boldSystemFontOfSize:22*screenW_6] andLineSpacing:10*screenW_6] - 22*screenW_6 <= 10*screenW_6) {
        h += 22*screenW_6;
    }else{
        h += [TomamaUI getStringHeightWith:_data[@"title"] andWeith:SCREENW - 30*screenW_6 andFont:[UIFont boldSystemFontOfSize:22*screenW_6] andLineSpacing:10*screenW_6];
    }

    return 294*screenW_6 + h;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return .1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HBHWebViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HBHWebViewCell"];
    if (!cell) {
        cell = [[HBHWebViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HBHWebViewCell"];
    }
    [cell loadCellWithWeb:self.web];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _webH;
}

-(void)getDetail
{
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/banner/details" Dic:@{@"id":_bannerId} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    _data = responseDic[@"data"];
                    NSString *content = [NSString stringWithFormat:@"<head><style>img{max-width:%fpx ;height: auto !important}</style></head>%@",SCREENW - 20,_data[@"content"]];
                    [self.web loadHTMLString:content baseURL:nil];
                    [_table reloadData];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

@end
